package org.NooLab.glue.instances;

import java.util.Observer;
import java.util.Vector;

import org.NooLab.glue.components.pkg.TaskPackage;
 
import org.NooLab.glue.subscription.FuturesIntf;
import org.NooLab.glue.transaction.Transaction;



class SourceParticipant extends MessageBoardParticipant implements ParticipantSourceIntf{

	TaskPackage taskPackage ;
	
	
	public SourceParticipant( ParticipantFactory pfac, String mbconfigpath, String name) {
		super(mbconfigpath, name);
		
		factory = pfac;
		
	}
	
	 
	
	@Override
	public void setTask(TaskPackage taskpackage) {
		taskPackage = taskpackage;
	}

	
	@Override
	public TaskPackage digestingReceivedMsg(Vector<TaskPackage> resultPackages ) {
	 
		 long transactionID;
		 
												out.printErr(4, "ParticipantFactory :: SourceParticipant :: digestingReceivedMsg()");
			
			
			for (int i=0;i<resultPackages.size();i++){
			
				transactionID = resultPackages.get(i).getTransactionID() ;
				setTransactionState( transactionID, Transaction.TS_CLOSED );
			}
			super.mbf.getTransactions().save();
			
			
			factory.resultsDeliveryStation.resultsProvider( resultPackages );
			
			return resultPackage;
	}

 
	@Override
	public TaskPackage digestingReceivedMsg(TaskPackage taskPackage) {
		 
		return null;
	}

	@Override
	public void currentStateMessage(String str) {
		 
		out.print(6, "routing state message (a)...");
		factory.resultsDeliveryStation.currentStateMessage(str) ;
	}


	@Override
	public void connect() {
		 
		connect(participantsRole, false);
		
		factory.connected = factory.xRegistration != null ;
	}

 
	public void send(){
		
		send(0) ;
	}




	@Override
	public void send(String xmlout, Observer obs) {
		// TODO Auto-generated method stub
		send(  xmlout , 0, obs) ;
	}



	@Override
	public void send(int routableHops) {
	 
		if ((factory.connected) && (taskPackage!=null)){
			send( taskPackage, null, routableHops ) ;
		}
	}



	@Override
	public void send(String xmlout) {
		 
		send(  xmlout , 0,null) ;
	}



	@Override
	public void send(String xmlout, int routableHops, Observer obs) {
		
		super.send( xmlout, routableHops,obs) ;
		
	}

	@Override
	public void setResultsCompletionMarker() {
		  
		super.setResultsCompletionMarker();
	}

	@Override
	public void disconnect() {
		 
		stop(); 
	}

	@Override
	public void retrieveAll() {
		 
		super.retrieveAll();
	}



	@Override
	public void cancelAll() {
		 
		super.cancelAll();
	}



	@Override
	public int cancelTask() {
		 
		return cancel();
	}
	
	public int cancelTask( long transactionID){
		return cancel(transactionID);
	}



	@Override
	public void handlingFailedTransactions( Transaction transaction ){
		long transactionID ;
		 
		// the transaction contains all data
		transactionID = transaction.getTransactionID() ;
		
		out.printErr(2, "handling failed transaction (id:"+transactionID+"), "+
						"all reasons are known... -> just drop it, so far...");
		// now we can do anything with the postponed transaction ....
		// retry... drop it... 
		
		setTransactionState( transactionID, Transaction.TS_TASK_CANCELED );
	}


	@Override
	public Vector<Long> getOpenedTransactionsID() {
		Vector<Long> itemsList = new Vector<Long>();
		
		//itemsList = this.getOpenedTransactionsIDs();
		
		return itemsList;
	}



	@Override
	public void requestAliveNotifications( long period ) {
		
		sendRequestForAliveNotifications();
	}



	@Override
	public void setControlPackage() {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void setContextPackage() {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void handlingCancelRequest( TaskPackage taskPackage , int marker ) {
		long transactionID;
		String str;
		
		transactionID = taskPackage.getTransactionID() ;
		str = "Control task has been confirmed, transaction (tid:"+transactionID+") has been canceled.";
		
		// "removing" this transaction
		setTransactionState( transactionID, Transaction.TS_TASK_CANCELED );
		
		out.print(2, "routing state message (b)...");
		// accessing the accessible client through the factory, which has been created by it
		((ResultReceiverIntf)(factory.parent)).currentStateMessage(str) ;
	}
  

	@Override
	public FuturesIntf getFutures() {
		
		return getFutures(0);
	}



	@Override
	public FuturesIntf getFutures(int mode) {
		FuturesIntf futures ;
		
		futures = super.getFutures(mode);
		
		
		
		return futures;
	}

 
}
